package health_system.health_system.controller;

import health_system.health_system.dto.ApiResponse;
import health_system.health_system.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * Check if a username exists
     * GET /api/users/check/username?value=username
     *
     * @param username the username to check
     * @return response with exists flag
     */
    @GetMapping("/check/username")
    public ResponseEntity<?> checkUsernameExists(@RequestParam("value") String username) {
        log.debug("REST request to check if username exists: {}", username);
        
        boolean exists = userService.existsByUsername(username);
        Map<String, Boolean> response = new HashMap<>();
        response.put("exists", exists);
        
        return ResponseEntity.ok(ApiResponse.success("Username check completed", response));
    }

    /**
     * Check if an email exists
     * GET /api/users/check/email?value=email
     *
     * @param email the email to check
     * @return response with exists flag
     */
    @GetMapping("/check/email")
    public ResponseEntity<?> checkEmailExists(@RequestParam("value") String email) {
        log.debug("REST request to check if email exists: {}", email);
        
        boolean exists = userService.existsByEmail(email);
        Map<String, Boolean> response = new HashMap<>();
        response.put("exists", exists);
        
        return ResponseEntity.ok(ApiResponse.success("Email check completed", response));
    }

    /**
     * Check if a phone number exists
     * GET /api/users/check/phone?value=phoneNumber
     *
     * @param phoneNumber the phone number to check
     * @return response with exists flag
     */
    @GetMapping("/check/phone")
    public ResponseEntity<?> checkPhoneNumberExists(@RequestParam("value") String phoneNumber) {
        log.debug("REST request to check if phone number exists: {}", phoneNumber);
        
        boolean exists = userService.existsByPhoneNumber(phoneNumber);
        Map<String, Boolean> response = new HashMap<>();
        response.put("exists", exists);
        return ResponseEntity.ok(ApiResponse.success("Phone number check completed", response));
    }
} 